﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;

namespace YandexWebmaster
{
    public partial class BD
    {
        public Account GetAccount(string login)
        {
            dynamic result = GetTable("sites", $"SELECT * FROM accounts WHERE login='{login}'")
                .AsEnumerable()
                .Select(
                    t => new Account
                    {
                        ID = Convert.ToInt32(t["id"]),
                        Login = t["login"].ToString(),
                        OauthToken = t["oauth_token"].ToString(),
                        Epires_in = Convert.ToUInt32(t["expires_in"])
                    }
                ).Single();
            return result;
        }

        public List<Account> GetAccounts()
        {
            dynamic result = GetTable("accounts", $"SELECT * FROM accounts")
                .AsEnumerable()
                .Select(
                    t => new Account
                    {
                        ID = Convert.ToInt32(t["id"]),
                        Login = t["login"].ToString(),
                        OauthToken = t["oauth_token"].ToString(),
                        Epires_in = Convert.ToUInt32(t["expires_in"])
                    }
                ).ToList();
            return result;
        }

        public void UpdateAccessToken(int id, string oauth_token, uint expires_in)
        {
            using (var connection = new SQLiteConnection($"Data Source={Directory.GetCurrentDirectory()}\\{Name};Version=3;"))
            {
                string query = $"UPDATE accounts SET oauth_token='{oauth_token}', expires_in='{expires_in}' WHERE id='{id}'";
                connection.Open();
                SQLiteCommand command = new SQLiteCommand(connection);
                command.CommandText = query;
                command.ExecuteNonQuery();
            }
        }

        public Account InsertAccount(Account account)
        {
            using (var connection = new SQLiteConnection($"Data Source={Directory.GetCurrentDirectory()}\\{Name};Version=3;"))
            {
                string query = $"INSERT INTO accounts VALUES ('{account.Login}', '', 0)";
                connection.Open();
                SQLiteCommand command = new SQLiteCommand(connection);
                command.CommandText = query;
                command.ExecuteNonQuery();
                return GetAccount(account.Login);
            }
        }

        public Account InsertAccount(string login)
        {
            using (var connection = new SQLiteConnection($"Data Source={Directory.GetCurrentDirectory()}\\{Name};Version=3;"))
            {
                string query = $"INSERT INTO accounts (login, oauth_token, expires_in) VALUES ('{login}', '', 0)";
                connection.Open();
                SQLiteCommand command = new SQLiteCommand(connection);
                command.CommandText = query;
                command.ExecuteNonQuery();
                return GetAccount(login);
            }
        }
    }
}
